﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace RegulatoryPlatform.Tools
{
    public class FileHelper
    {
        public static string ReadJson(string jsonfile)
        {

            using (StreamReader file = File.OpenText(jsonfile))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    return o.ToString();
                }
            }
        }
        /// <summary>
        /// 按行读取文本文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> ReadLine(string path, Encoding encoding = null)
        {
            List<string> list = new List<string>();
            using (StreamReader sr = new StreamReader(path, encoding == null ? Encoding.Default : encoding))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    list.Add(line);
                }
            }
            return list;
        }
        /// <summary>
        /// 读取文本文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadTxt(string path)
        {
            using (StreamReader sr = new StreamReader(path, Encoding.Default))
            {
                return sr.ReadToEnd();
            }
        }
        /// <summary>
        /// 追加
        /// </summary>
        /// <param name="lines"></param>
        /// <param name="path"></param>
        public static void AppendSaveLine(List<string> lines, string path)
        {
            using (StreamWriter sw = new StreamWriter(path, true))
            {
                foreach (var line in lines)
                {
                    sw.WriteLine(line);
                }
            }
        }
        public static void AppendSave(string content, string path)
        {
            using (StreamWriter sw = new StreamWriter(path, true))
            {
                sw.Write(content);
            }
        }
        public static void Save(string content, string path)
        {
            Clear(path);
            using (StreamWriter sw = new StreamWriter(path, true))
            {
                sw.Write(content);
            }
        }
        public static void SaveLine(List<string> lines, string path)
        {
            Clear(path);
            using (StreamWriter sw = new StreamWriter(path, true))
            {
                foreach (var line in lines)
                {
                    sw.WriteLine(line);
                }
            }
        }
        public static void Clear(string path)
        {
            using (FileStream stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.SetLength(0); //清空txt文件
            }
        }
        public static FileInfo[] GetDirectoryFiles(string path)
        {
            DirectoryInfo root = new DirectoryInfo(path);
            return root.GetFiles();
        }
    }
}
